package com.example.wishstar.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.wishstar.MyApplication;
import com.example.wishstar.db.UserDatabase;
import com.example.wishstar.db.UserDao;
import com.example.wishstar.model.Users;
import com.example.wishstar.repository.UsersRepository;

public class UsersViewModel extends AndroidViewModel {

    private LiveData<Users> usersLiveData;
    private UsersRepository usersRepository;
    private int userId;

    public UsersViewModel(@NonNull Application application) {
        super(application);
        UserDatabase database = MyApplication.getUserDatabase();
        UserDao userDao = database.userDao();
        usersRepository = new UsersRepository(userDao, MyApplication.getApi());
        userId = MyApplication.userId;
        usersLiveData = usersRepository.getUsers(userId);
    }

    public LiveData<Users> getUsersLiveData() {
        return usersLiveData;
    }

    public void refresh() {
        usersRepository.refresh(userId);
    }

    public void logOut() {
        usersRepository.logOut(userId);
    }
}
